// Fill out your copyright notice in the Description page of Project Settings.

#include "BTTask_OnAttack.h"
#include "SimpleAIController.h"
#include "SimpleAIPawn.h"
#include "NavigationSystem.h"




UBTTask_OnAttack::UBTTask_OnAttack(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{}

EBTNodeResult::Type UBTTask_OnAttack::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
	ASimpleAIController* BotComponent = Cast<ASimpleAIController>(OwnerComp.GetAIOwner());
	if (!BotComponent) return EBTNodeResult::Failed;
	ASimpleAIPawn* Bot = Cast<ASimpleAIPawn>(BotComponent->GetPawn());
	if (Bot->ShootBy.Get())
	{
		const float SearchRadius = 1000.0f;
		const FVector SearchOrigin = Bot->ShootBy->GetActorLocation();
		FVector ShootLocation(0);
		ShootLocation = UNavigationSystemV1::GetRandomPointInNavigableRadius(GetWorld(), SearchOrigin, SearchRadius);
		

		EPathFollowingRequestResult::Type MoveTORec;
		MoveTORec = BotComponent->MoveToLocation(ShootLocation, 200.f);
		if (MoveTORec == EPathFollowingRequestResult::Failed)
		{
			return EBTNodeResult::Failed;
		} else if (MoveTORec == EPathFollowingRequestResult::RequestSuccessful)
		{
			BotComponent->SetFocus(Bot->ShootBy.Get());
			BotComponent->ShootEnemy();
			return EBTNodeResult::Succeeded;
		}
		
	}

	return EBTNodeResult::Failed;
}
